#!/usr/bin/env python
# coding: utf-8

import gtk, gtk.glade
import gobject
import os
import cPickle as pickle
#from dictfile import DictFile

(
    COLUMN_TITLE,
    COLUMN_GROUP,
    COLUMN_NUM,
    COLUMN_TIMES,
    COLUMN_NEXT,
) = range(5)

COLUMN_FINISH = COLUMN_TIMES

class Result():
    def __init__(self, wTree):
        self.rr = None
        self.ing = 0
        self.finished = 0
        self.wTree = wTree

        label_score = self.wTree.get_widget("label_score")
        label_score.set_markup('<span size="xx-large">你的成绩</span>')

        self.label_ing = self.wTree.get_widget("label_ing")
        self.label_finished = self.wTree.get_widget("label_finished")
        self.treeview_ing = self.wTree.get_widget("treeview_ing")
        self.treeview_finished = self.wTree.get_widget("treeview_finished")
        
        progress = self.create_progress_list()
        finished = self.create_finished_list()

    def create_model(self):
        self.create_progress_model()
        self.create_finished_model()

    def create_progress_list(self, finish = None):


        self.treeview_ing.progress_model = gtk.ListStore(
                gobject.TYPE_STRING,
                gobject.TYPE_STRING,
                gobject.TYPE_STRING,
                gobject.TYPE_STRING,
                gobject.TYPE_STRING)

        self.treeview_ing.set_model(self.treeview_ing.progress_model)

        self.create_progress_model()

        self.__add_title_column(self.treeview_ing, "书名",COLUMN_TITLE)
        self.__add_title_column(self.treeview_ing, "组别",COLUMN_GROUP)
        self.__add_title_column(self.treeview_ing, "单词数",COLUMN_NUM)
        self.__add_title_column(self.treeview_ing, "剩余复习次数",COLUMN_TIMES)
        self.__add_title_column(self.treeview_ing, "下次复习时间",COLUMN_NEXT)

    def __add_title_column(self, treeview, title, column_id):
        column = gtk.TreeViewColumn(title, gtk.CellRendererText() , text = COLUMN_NEXT)
        treeview.append_column(column)

    def create_progress_model(self):
        self.treeview_ing.progress_model.clear()
        self.ing = 0
        f = file(os.path.join(os.path.expanduser("~"), ".wordjoy/record"), "rb")
        Loading = True

        while Loading:
            try:
                rr = pickle.load(f)
            except pickle.UnpicklingError:
                pass
            except EOFError:
                Loading = False
            else:
                iter = self.treeview_ing.progress_model.append()
                self.ing += len(rr.words)
                self.treeview_ing.progress_model.set(iter,
                    COLUMN_TITLE, rr.get_dict().INFO["TITLE"],
                    COLUMN_GROUP, rr.group,
                    COLUMN_NUM, len(rr.words),
                    COLUMN_TIMES, 7 - rr.time,
                    COLUMN_NEXT, rr.next)
        self.label_ing.set_markup("你正在强化记忆<b>%d</b>个单词" % self.ing)
        f.close()

    def create_finished_list(self, finish = None):
        self.finished_model = gtk.ListStore(
                gobject.TYPE_STRING,
                gobject.TYPE_STRING,
                gobject.TYPE_STRING,
                gobject.TYPE_STRING)

        self.treeview_finished.set_model(self.finished_model)

        self.create_finished_model()

        self.__add_title_column(self.treeview_finished, "书名",COLUMN_TITLE)
        self.__add_title_column(self.treeview_finished, "组别",COLUMN_GROUP)
        self.__add_title_column(self.treeview_finished, "单词数", COLUMN_NUM)
        self.__add_title_column(self.treeview_finished, "完成时间", COLUMN_NUM)

    def create_finished_model(self):
        self.finished_model.clear()
        self.finished = 0
        f = file(os.path.join(os.path.expanduser("~"), ".wordjoy/finished"), "rb")
        Loading = True

        while Loading:
            try:
                rr = pickle.load(f)
            except pickle.UnpicklingError:
                pass
            except EOFError:
                Loading = False
            else:
                iter = self.finished_model.append()
                self.finished += len(rr.words)
                self.finished_model.set(iter,
                    COLUMN_TITLE, rr.get_dict().INFO["TITLE"],
                    COLUMN_GROUP, rr.group,
                    COLUMN_NUM, len(rr.words),
                    COLUMN_FINISH, rr.next)
        self.label_finished.set_markup("你已经掌握了<b>%d</b>个单词" % self.finished)
        f.close()

if __name__ == "__main__":
    glade_file = "../glade/result_test.glade"
    wTree = gtk.glade.XML(glade_file)

    
    win = wTree.get_widget("window_result")
    win.connect('destroy', lambda *w: gtk.main_quit())
    
    Result(wTree)

    gtk.main()
